package com.ruoyi.gd.qybjklog.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.gd.wgd.domain.ZWgd;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.gd.qybjklog.domain.bo.QybjkLogBo;
import com.ruoyi.gd.qybjklog.domain.vo.QybjkLogVo;
import com.ruoyi.gd.qybjklog.domain.QybjkLog;
import com.ruoyi.gd.qybjklog.mapper.QybjkLogMapper;
import com.ruoyi.gd.qybjklog.service.IQybjkLogService;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 企业报价库日志Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-20
 */
@RequiredArgsConstructor
@Service
public class QybjkLogServiceImpl implements IQybjkLogService {

    private final QybjkLogMapper baseMapper;

    /**
     * 查询企业报价库日志
     */
    @Override
    public QybjkLogVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询企业报价库日志列表
     */
    @Override
    public TableDataInfo<QybjkLogVo> queryPageList(QybjkLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<QybjkLog> lqw = buildQueryWrapper(bo);
        Page<QybjkLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询企业报价库日志列表
     */
    @Override
    public List<QybjkLogVo> queryList(QybjkLogBo bo) {
        LambdaQueryWrapper<QybjkLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<QybjkLog> buildQueryWrapper(QybjkLogBo bo) {
        LambdaQueryWrapper<QybjkLog> lqw = Wrappers.lambdaQuery();
       try {
           lqw.eq(StringUtils.isNotBlank(bo.getYwid()), QybjkLog::getYwid, bo.getYwid());
           lqw.eq(StringUtils.isNotBlank(bo.getMsg()), QybjkLog::getMsg, bo.getMsg());
           // 判断当前的状态
           String status = bo.getStatus();
           if (StringUtils.isNotEmpty(status)) {
               List<String> strings = Arrays.asList(bo.getStatus().split(","));
               lqw.in(QybjkLog::getStatus, strings);
           }
           // 添加时间条件
           if (bo.getParams()!=null && bo.getParams().size() > 0) {
               SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
               Object start = bo.getParams().get("startTime");
               Object end = bo.getParams().get("endTime");
               if (null != start && null != end) {
                   Date startTime = format.parse((String) start);
                   Date endTime = format.parse((String) end);
                   if (startTime != null && endTime != null) {
                       lqw.ge(QybjkLog::getCreateTime, startTime);
                       lqw.le(QybjkLog::getCreateTime, endTime);
                   }
               }
           }
           lqw.orderByDesc(QybjkLog::getStatus);
           lqw.orderByDesc(QybjkLog::getCreateTime);
       }catch (Exception e) {
           e.printStackTrace();
       }
        return lqw;
    }

    /**
     * 新增企业报价库日志
     */
    @Override
    public Boolean insertByBo(QybjkLogBo bo) {
        QybjkLog add = BeanUtil.toBean(bo, QybjkLog.class);
        validEntityBeforeSave(add);
        return baseMapper.insert(add) > 0;
    }

    /**
     * 修改企业报价库日志
     */
    @Override
    public Boolean updateByBo(QybjkLogBo bo) {
        QybjkLog update = BeanUtil.toBean(bo, QybjkLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(QybjkLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除企业报价库日志
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
